package network;
//import java.io.IOException;
//import java.net.DatagramPacket;
//import java.net.DatagramSocket;
//import java.net.InetAddress;
//import java.net.SocketException;
//import java.util.Scanner;
//
//public class UdpEchoClient {
//    private DatagramSocket socket = null;
//    private String serverIp;
//    private int serverPort;
//    public UdpEchoClient(String serverIp, int serverPort) throws SocketException {
//        this.serverIp = serverIp;
//        this.serverPort = serverPort;
//        socket = new DatagramSocket();
//    }
//
//    public void start() throws IOException {
//        System.out.println("客户端启动!");
//        Scanner scanner = new Scanner(System.in);
//        while(true) {
//            System.out.print("->");
//            //1.从控制台读取要发送的数据
//            if(!scanner.hasNext()) {
//                break;
//            }
//            String request = scanner.next();
//            //2.构造请求并发送
//            DatagramPacket responsesPacket = new DatagramPacket(request.getBytes(),request.getBytes().length,
//                    InetAddress.getByName(serverIp),serverPort);
//            socket.send(responsesPacket);
//            //3.读取服务器的响应
//            DatagramPacket datagramPacket = new DatagramPacket(new byte[4096],4096);
//            socket.receive(responsesPacket);
//            //4.把响应显示到控制台
//            String response = new String(responsesPacket.getData(),0,responsesPacket.getLength());
//            System.out.println(response);
//        }
//    }
//
//    public static void main(String[] args) throws IOException {
//        UdpEchoClient client = new UdpEchoClient("127.0.0.1",9090);
//        client.start();
//    }
//}


import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient {
    public DatagramSocket socket = null;
    private String serverIp;
    private int serverPort;

    public UdpEchoClient(String serverIp, int serverPort) throws SocketException {
        this.serverIp = serverIp;
        this.serverPort = serverPort;
        socket = new DatagramSocket();
    }

    public void start() throws IOException {
        System.out.println("客户端启动!");
        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.print("->");
            if(!scanner.hasNext()) {
                break;
            }
            String request = scanner.next();
            DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),
                    0,request.getBytes().length, InetAddress.getByName(serverIp), serverPort);
            socket.send(requestPacket);
            DatagramPacket responsePacket = new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
            String response = new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println(response);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient client = new UdpEchoClient("127.0.0.1",9090);
        client.start();
    }
}